package com.example.demo.controller;


import com.example.demo.bean.FadgMxDyEntity;
import com.example.demo.config.ExcelUtil;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: SpringAOP
 * @ClassName ExportController
 * @description:
 * @author: zsn
 * @create: 2024-07-09 09:02
 * @Version 1.0
 **/
@RestController
@RequestMapping("/export")
public class ExportController {

    @RequestMapping("/excel")
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取数据
//        Map<String, Object> data = requirementService.query(null);
//        List<Requirement> list = (List<Requirement>) data.get("requirements");
        ArrayList<Object> list = new ArrayList<>();
        list.add(1);
//        FadgMxDyEntity fadgMxDyEntity = new FadgMxDyEntity();
//        fadgMxDyEntity.setCompany("国网河南省电力公司郑州供电公司");
//        fadgMxDyEntity.setCity("郑州");
//        fadgMxDyEntity.setCounty("中牟");
//        fadgMxDyEntity.setYwdwmc("国网河南中牟县供电公司");
//        fadgMxDyEntity.setFhdyictjmxs(10);
//        fadgMxDyEntity.setCjdymxs(5);
//        fadgMxDyEntity.setWcjdymxs(10);
//        fadgMxDyEntity.setSjyxmxs(15);
//        fadgMxDyEntity.setCjl(20);
//        fadgMxDyEntity.setYsxsj("0");
//        fadgMxDyEntity.setYxxsj("0");
//        fadgMxDyEntity.setZhdyhgl(99.9);
//        fadgMxDyEntity.setDateType(0);
//        fadgMxDyEntity.setDate("2024-07-15");
//        fadgMxDyEntity.setAddTime(1721010030L);
//        fadgMxDyEntity.setUpdateTime(1721010030L);
//        fadgMxDyEntity.setZhdyhgl(12345678.0);
//        list.add(fadgMxDyEntity);

        //excel标题
        String[] title = {"单位名称", "运维单位", "运维班组", "设备名称", "所属变电站", "电压最大值（V）", "最大发生时间", "最小值（V）", "最小发生时间", "平均值", "是否越限", "越上限时间（分）", "越下线时间（分）", "监测总时间", "越上线指数",
                "越下线指数"};
        //excel文件名
        String fileName = "母线电压详细数据查询" + System.currentTimeMillis() + ".xls";
        //sheet名
        String sheetName = "母线电压详细数据查询";

        String[][] content = new String[list.size() + 1][title.length];
        for (int i = 0; i < list.size(); i++) {
//            Requirement r = list.get(i);
//            System.out.println(r.toString());
            content[i][0] = "国网河南省电力公司郑州供电公司";
            content[i][1] = "国网河南中牟县供电公司";
            content[i][2] = "沙河供电所";
            content[i][3] = "里10kv西母母线";
            content[i][4] = "沙河里变电站";
            content[i][5] = "100kV";
            content[i][6] = "2分钟";
            content[i][7] = "1kV";
            content[i][8] = "10s";
            content[i][9] = "20kV";
            content[i][10] = "是";
            content[i][11] = "2分钟";
            content[i][12] = "1分钟";
            content[i][13] = "1h";
            content[i][14] = "70";
            content[i][15] = "80";
        }

        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
//            EasyExcel.write(os, FadgMxDyEntity.class)
//                    .sheet(sheetName)
//                    .doWrite(list);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

//    @RequestMapping("/excel")
//    @ResponseBody
//    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        //获取数据
////        Map<String, Object> data = requirementService.query(null);
////        List<Requirement> list = (List<Requirement>) data.get("requirements");
//        ArrayList<FadgMxDyEntity> list = new ArrayList<>();
//        FadgMxDyEntity fadgMxDyEntity = new FadgMxDyEntity();
//        fadgMxDyEntity.setCompany("国网河南省电力公司郑州供电公司");
//        fadgMxDyEntity.setCity("郑州");
//        fadgMxDyEntity.setCounty("中牟");
//        fadgMxDyEntity.setYwdwmc("国网河南中牟县供电公司");
//        fadgMxDyEntity.setFhdyictjmxs(10);
//        fadgMxDyEntity.setCjdymxs(5);
//        fadgMxDyEntity.setWcjdymxs(10);
//        fadgMxDyEntity.setSjyxmxs(15);
//        fadgMxDyEntity.setCjl(20);
//        fadgMxDyEntity.setYsxsj("0");
//        fadgMxDyEntity.setYxxsj("0");
//        fadgMxDyEntity.setZhdyhgl(99.9);
//        fadgMxDyEntity.setDateType(0);
//        fadgMxDyEntity.setDate("2024-07-15");
//        fadgMxDyEntity.setAddTime(1721010030L);
//        fadgMxDyEntity.setUpdateTime(1721010030L);
//        fadgMxDyEntity.setZhdyhgl(12345678.0);
//        list.add(fadgMxDyEntity);
//
//        //excel标题
//        //        String[] title = {"单位名称", "运维单位", "运维班组", "设备名称", "所属变电站", "电压最大值（V）", "最大发生时间", "最小值（V）", "最小发生时间", "平均值", "是否越限", "越上限时间（分）", "越下线时间（分）", "监测总时间", "越上线指数",
//        //                "越下线指数"};
//        //        //excel文件名
//        String fileName = "母线电压详细数据查询" + System.currentTimeMillis() + ".xls";
//        //sheet名
//        String sheetName = "母线电压详细数据查询";
//
////        String[][] content = new String[list.size() + 1][title.length];
////        for (int i = 0; i < list.size(); i++) {
//////            Requirement r = list.get(i);
//////            System.out.println(r.toString());
////            content[i][0] = "国网河南省电力公司郑州供电公司";
////            content[i][1] = "国网河南中牟县供电公司";
////            content[i][2] = "沙河供电所";
////            content[i][3] = "里10kv西母母线";
////            content[i][4] = "沙河里变电站";
////            content[i][5] = "100kV";
////            content[i][6] = "2分钟";
////            content[i][7] = "1kV";
////            content[i][8] = "10s";
////            content[i][9] = "20kV";
////            content[i][10] = "是";
////            content[i][11] = "2分钟";
////            content[i][12] = "1分钟";
////            content[i][13] = "1h";
////            content[i][14] = "70";
////            content[i][15] = "80";
////        }
//
//        //创建HSSFWorkbook
////        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
//        //响应到客户端
//        try {
//            this.setResponseHeader(response, fileName);
//            OutputStream os = response.getOutputStream();
////            wb.write(os);
//            EasyExcel.write(os, FadgMxDyEntity.class)
//                    .sheet(sheetName)
//                    .doWrite(list);
//            os.flush();
//            os.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }




}